package top.xana.acg.anime.ui.page

import androidx.fragment.app.activityViewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.drake.brv.BR
import com.drake.brv.BindingAdapter
import com.drake.brv.utils.grid
import top.xana.acg.anime.R
import top.xana.acg.anime.SearchSource
import top.xana.acg.anime.databinding.HomeFragmentBinding
import top.xana.acg.anime.model.*
import top.xana.acg.anime.ui.viewmodel.AnimeViewModel
import top.xana.acg.anime.utils.navTo
import top.xana.acg.base.BaseFragment

class HomeFragment : BaseFragment<HomeFragmentBinding>(R.layout.home_fragment) {

    companion object {
        const val TAG = "HomeFragment"

        @JvmStatic
        fun newInstance() = HomeFragment()

    }

    private val viewModel by activityViewModels<AnimeViewModel>()

    private var pageAdapt: PageAdapter? = null

    override fun initView() {
        super.initView()
        binding.vm = viewModel
        pageAdapt = PageAdapter()
        binding.homeView.grid().adapter = pageAdapt
    }

    private class PageAdapter : BindingAdapter() {
        init {
            setup()
        }

        private val spanCount = 12

        private fun setup() {
            addType<LabelUi>(R.layout.item_label_bar)
            addType<CollectionUi>(R.layout.view_recycler_view)
            addType<Anime> {
                when (src) {
                    Anime.SOURCE_COLLECTION -> R.layout.item_favor_anime_view
                    Anime.SOURCE_HOT -> R.layout.item_hot_anime_view
                    Anime.SOURCE_TIMELINE -> R.layout.item_time_line_anime_view
                    Anime.SOURCE_COMMON -> R.layout.item_category_anime_view
                    else -> R.layout.item_category_anime_view
                }
            }
            addType<BannerUi>(R.layout.item_home_banner_anime_view)

            onBind {
                val entity = getModelOrNull<Any>()
                modelId = when (entity) {
                    is Anime -> BR.anime
                    is LabelUi -> BR.label
                    else -> BR.anime
                }
            }

            onClick(R.id.frameAnime) {
                val model = getModel<Anime>()
                itemView.navTo(
                    R.id.toPagePlayer,
                    AnimePlayerFragment.createBundle(SearchSource.Bimi, model)
                )
            }
        }

        private val sizeLookup = object : GridLayoutManager.SpanSizeLookup() {
            override fun getSpanSize(position: Int): Int {
                return when (val entity = getModelOrNull<Any>(position)) {
                    is BannerUi,
                    is LabelUi,
                    is TimelineUi -> spanCount

                    is Anime -> when (entity.src) {
                        Anime.SOURCE_HOT -> spanCount / 2
                        Anime.SOURCE_COMMON -> spanCount / 3
                        else -> spanCount
                    }
                    else -> spanCount
                }
            }
        }

        override fun onAttachedToRecyclerView(recyclerView: RecyclerView) {
            super.onAttachedToRecyclerView(recyclerView)
            val lm = recyclerView.layoutManager
            if (lm is GridLayoutManager) {
                lm.spanCount = spanCount
                lm.spanSizeLookup = sizeLookup
            }
        }
    }

}